
var roleId;//角色ID
$(function () {
    roleId = $.getUrlParam("id");

    $('#roleForm').validationEngine('attach', {
        validationEventTrigger : 'blur',
        promptPosition: 'topRight',
        scroll: true,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
            if (valid){
                if (roleId == 'null') {
                    saveRole();
                } else {
                    modifyRole();
                }
            }
        }
    });
    /*$.ajax({
        url: "/doublesys/role/getCurrUserRole.htm",
        cache: false,
        type: "post",
        dataType: "json",
        async:false,
        data: {id: roleId},
        success: function (result) {
            if (result.status == 0) {
                var data = result.data;
                var level = data.level ? parseInt(data.level)+1 : 0;//所操作等级不能>=当前登陆用户角色等级,最大等级+1 ： 新增等级
                $('.spinner input').val(level);
                $("#level").addClass("validate[required,max["+(data.maxLevel+1)+"],min["+(level)+"],custom[number]]");
                return;
            }
        }
    });*/
    if(roleId == 'null'){
        return;
    }
    
    //初始化角色信息
    $.ajax({
        url: "../role/getRoleInfoById.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: {id: roleId},
        success: function (result) {
            if (result.status == 0) {
                var data = result.data;
                $("#roleForm").formatForm(data);
                if(data.roleName == 'admin' || data.roleName == 'root'){
                    $("#roleName").attr("disabled","true");
                }
                return;
            }
            parent.layer.msg(result.message, {icon: 2});
        }
    });

});

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

//修改角色
function modifyRole() {
    $.ajax({
        url: "../role/modifyRoleInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#roleForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../role/getRolesPage.htm'});
                closeForm();
            }else{
                parent.layer.msg(result.message, {icon: 2});
            }
        }
    });
}

//新增角色
function saveRole() {
    $.ajax({
        url: "../role/addToRoleInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#roleForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../role/getRolesPage.htm'});
                closeForm();
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
        }
    });
}
